﻿Shader "Unlit/UnlitShader(1)"
{
    Properties
    {
        //xPixels ("Texture", 2D) = "white" {}
        xPixels("xPixels", 2D) = "white" {}
        xPalette("xPalette", 2D) = "white" {}
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" }
        ZWrite Off // 关闭深度写入
        Blend SrcAlpha OneMinusSrcAlpha // 混合的参数
        LOD 100

        CGINCLUDE
        #define SAMPLE_TEXTURE(Name, texCoord)  tex2D(Name, texCoord)
        // make fog work
        #pragma multi_compile_fog
        #include "UnityCG.cginc"
        struct appdata
        {
            float4 vertex : POSITION;
            float2 uv : TEXCOORD0;
            float4 Color: COLOR0;
        };

        struct v2f
        {
            float2 uv : TEXCOORD0;
            UNITY_FOG_COORDS(1)
            float4 vertex : SV_POSITION;
            float4 Color: COLOR0;
        };

        sampler2D xPixels;
        sampler2D xPalette;
        float4 xPixels_ST;

        float4x4 xMatrix;
        float4x4 xRotation;

        float xBlendWeight;

        int xFontColorIndex;
        int xFontTotalColors;

        int xPalFx_Use;
        float4 xPalFx_Add;
        float4 xPalFx_Mul;
        int xPalFx_Invert;
        float xPalFx_Color;
        float4 xPalFx_SinMath;

        int xAI_Use;
        int xAI_Invert;
        float xAI_color;
        float4 xAI_preadd;
        float4 xAI_contrast;
        float4 xAI_postadd;
        float4 xAI_paladd;
        float4 xAI_palmul;
        int xAI_number;

        float4 xShadowColor;

        float4 BaseColor(float4 inputcolor, float base)
        {
            if (base == 0.0f)
            {
                float color = (0.299f * inputcolor.r) + (0.587f * inputcolor.g) + (0.114f * inputcolor.b);
                return float4(color, color, color, inputcolor.a);
            }
            return float4(inputcolor.rgb * base, inputcolor.a);
        }

        float4 PalFx(float4 inputcolor)
        {
            float4 output = BaseColor(inputcolor, xPalFx_Color);
            if (xPalFx_Invert == 1) output = float4(1 - output.rgba);
            output = (output + xPalFx_Add + xPalFx_SinMath) * xPalFx_Mul;
            return float4(output.bgr, inputcolor.a);
        }

        float4 AfterImage(float4 inputcolor)
        {
            float4 output = BaseColor(inputcolor, xAI_color);
            if (xAI_Invert == 1) output = float4(1 - output.rgba);
            output += xAI_preadd;
            output *= xAI_contrast;
            output += xAI_postadd;
            [unroll(25)]
            for (int i = 0; i <= xAI_number; ++i) output = (output + xAI_paladd) * xAI_palmul;
            return float4(output.bgr, inputcolor.a);
        }
        
        float4 AfterImageOLD(float4 inputcolor)
        {
            float4 output = BaseColor(inputcolor, xAI_color);
    
            if(xAI_Invert == true) output = float4(1 - output.rgba);
    
            output += xAI_preadd;
            output *= xAI_contrast;
            output += xAI_postadd;
    
            return float4(output.bgr, inputcolor.a);
        }


        v2f DefaultVertexShader (appdata v)
        {
            v2f o;
            //o.vertex = UnityObjectToClipPos(v.vertex);
            o.vertex= mul(mul(v.vertex, xMatrix), xRotation);
            //o.uv = TRANSFORM_TEX(v.uv, xPixels);
            o.uv = v.uv;
            o.Color = v.Color;
            //UNITY_TRANSFER_FOG(o,o.vertex);
            return o;
        }

        fixed4 DefaultPixelShader (v2f i) : SV_Target
        {
            //// sample the texture
            //fixed4 col = tex2D(xPixels, i.uv);
            //// apply fog
            //UNITY_APPLY_FOG(i.fogCoord, col);
            //return col;

            float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
            if (color_index == 0.0f) return float4(0, 0, 0, 0);

            float4 output_color = SAMPLE_TEXTURE(xPalette, color_index);

            if (xPalFx_Use == 1) output_color = PalFx(output_color);
            if (xAI_Use == 1) output_color = AfterImage(output_color);

            output_color *= i.Color;
            return float4(output_color.bgr, xBlendWeight);
        }

        float4 PixelShaderOLD(v2f i) : SV_Target
        {
            float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
            if(color_index == 0.0f) return float4(0, 0, 0, 0);

            float4 output_color = SAMPLE_TEXTURE(xPalette, color_index);
    
            if(xPalFx_Use == true) output_color = PalFx(output_color);
            if(xAI_Use == true) output_color = AfterImageOLD(output_color);
    
            output_color *= i.Color;
    
            return float4(output_color.bgr, xBlendWeight);
        }

        float4 FontPixelShader(v2f i) : SV_Target
        {
            float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
            if(color_index == 0.0f) return float4(0, 0, 0, 0);

            float per = 1.0 - float(xFontColorIndex) / float(xFontTotalColors);
            float4 output_color = SAMPLE_TEXTURE(xPalette, color_index * per - 1.0/255.0);
    
            output_color *= i.Color;
    
            return float4(output_color.bgr, xBlendWeight);
        }

        float4 ShadowPixelShader(v2f i) : SV_Target
        {
            float color_index = SAMPLE_TEXTURE(xPixels, i.uv).r;
    
            if(color_index == 0.0f)
            {
                return float4(0, 0, 0, 0);
            }
            return float4(xShadowColor.bgr, xBlendWeight);
        }
        ENDCG


        Pass
        {
            Name "Draw"
            CGPROGRAM
            #pragma vertex DefaultVertexShader
            #pragma fragment DefaultPixelShader
            ENDCG
        }
		Pass
        {
            Name "FontDraw"
            CGPROGRAM
            #pragma vertex DefaultVertexShader
            #pragma fragment FontPixelShader
            ENDCG
        }
            /*Pass
        {
            Name "DrawOLD"
            CGPROGRAM
            #pragma vertex DefaultVertexShader
            #pragma fragment PixelShaderOLD
            ENDCG
        }
        
        Pass
        {
            Name "ShadowDraw"
            CGPROGRAM
            #pragma vertex DefaultVertexShader
            #pragma fragment ShadowPixelShader
            ENDCG
        }*/
    }
    FallBack "Diffuse"
}
